﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using DTO;
namespace QLDT
{
    public partial class frmQLDeTai : DevExpress.XtraEditors.XtraForm
    {
        DTO_TaiKhoan taikhoan;
        bool ttThemSua = true;
        public frmQLDeTai(DTO_TaiKhoan tk)
        {
            taikhoan = new DTO_TaiKhoan();
            taikhoan = tk;
            InitializeComponent();
        }
        public frmQLDeTai()
        {
            InitializeComponent();
        }

        private void frmQLDeTai_Load(object sender, EventArgs e)
        {
            load();
        }
        public void createGridlookup()
        {
            
            // gridlookup Ma chuyen nganh
            gridLookMaCN.EditValue = null;
            gridLookMaCN.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookMaCN.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookMaCN.Properties.ValueMember = "MaCN";
            gridLookMaCN.Properties.DisplayMember = "MaCN";
            gridLookMaCN.Properties.View.BestFitColumns();
            gridLookMaCN.Properties.PopupFormWidth = 400;
            gridLookMaCN.Properties.NullText = "Chọn Chuyên Ngành";
            gridLookMaCN.Properties.DataSource = BUS_ChuyenNganh.SelectChuyenNganh();
            // gridlookup Ma loai de tai
            gridLookMaLoaiDT.EditValue = null;
            gridLookMaLoaiDT.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookMaLoaiDT.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookMaLoaiDT.Properties.ValueMember = "MaLoaiDT";
            gridLookMaLoaiDT.Properties.DisplayMember = "MaLoaiDT";
            gridLookMaLoaiDT.Properties.View.BestFitColumns();
            gridLookMaLoaiDT.Properties.PopupFormWidth = 400;
            gridLookMaLoaiDT.Properties.NullText = "Chọn loại đề tài";
            gridLookMaLoaiDT.Properties.DataSource = BUS_LoaiDT.selectLoaiDeTai();
        }
        public void load()
        {
            layout_CapNhatDT.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            gridDSDeTai.DataSource = BUS.BUS_DeTai.selectDeTai_TheoGiangVien(taikhoan.TenTK);
            txtMaDT.Enabled = true;
            createGridlookup();
            if (gvDSDeTai.RowCount > 0)
            {
                btnXoaDT.Enabled = true;
                btnSuaDT.Enabled = true;
            }
            else
            {
                btnXoaDT.Enabled = false;
                btnSuaDT.Enabled = false;
            }
            btnGuiDT.Enabled = true;
        }
        public void NullText()
        {
            txtMaDT.Text = "";
            txtTenDT.Text = "";
            txtSoSVToiDa.Text = "";
            txtSoSVHienhanh.Text = "";
            txtSoTuanthuchien.Text = "";
            txtHocKy.Text = "";
            txtNamhoc.Text = "";
            createGridlookup();
        }
        private void btnGuiDT_Click(object sender, EventArgs e)
        {
            layout_CapNhatDT.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            ttThemSua = true;
            txtMaDT.Enabled = true;
            NullText();
            lb_titleQLDT.Text = "GỬI ĐỀ TÀI";
            btnGuiDT.Enabled = false;
            btnXoaDT.Enabled = false;
            btnSuaDT.Enabled = false;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            layout_CapNhatDT.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            load();
        }

        private void txtMaDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void txtSoSVToiDa_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoSVHienhanh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoTuanthuchien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnSuaDT_Click(object sender, EventArgs e)
        {
            lb_titleQLDT.Text = "CHỈNH SỬA ĐỀ TÀI";
            if (gvDSDeTai.GetRowCellValue(gvDSDeTai.FocusedRowHandle, "TrangThai").ToString() == "True")
            {
                XtraMessageBox.Show("Đề tài đã được duyệt, không thể thay đổi","Thông báo");
                return;
            }
            btnGuiDT.Enabled = false;
            btnXoaDT.Enabled = false;
            btnSuaDT.Enabled = false;
            layout_CapNhatDT.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            txtMaDT.Text = gvDSDeTai.GetRowCellValue(gvDSDeTai.FocusedRowHandle, "MaDT").ToString();
            txtTenDT.Text =  gvDSDeTai.GetRowCellValue(gvDSDeTai.FocusedRowHandle, "TenDT").ToString();
            txtSoSVToiDa.Text = gvDSDeTai.GetRowCellValue(gvDSDeTai.FocusedRowHandle, "SVToiDa").ToString();
            txtSoSVHienhanh.Text = gvDSDeTai.GetRowCellValue(gvDSDeTai.FocusedRowHandle, "SoSVHH").ToString();
            txtSoTuanthuchien.Text = gvDSDeTai.GetRowCellValue(gvDSDeTai.FocusedRowHandle, "SoTuan").ToString();
            txtHocKy.Text = gvDSDeTai.GetRowCellValue(gvDSDeTai.FocusedRowHandle, "HocKy").ToString();
            txtNamhoc.Text = gvDSDeTai.GetRowCellValue(gvDSDeTai.FocusedRowHandle, "NamHoc").ToString();
            gridLookMaLoaiDT.EditValue = null;
            gridLookMaLoaiDT.Properties.NullText = gvDSDeTai.GetRowCellValue(gvDSDeTai.FocusedRowHandle, "MaLoaiDT").ToString();
            gridLookMaCN.EditValue = null;
            gridLookMaCN.Properties.NullText = gvDSDeTai.GetRowCellValue(gvDSDeTai.FocusedRowHandle, "MaCN").ToString();
            ttThemSua = false;
            txtMaDT.Enabled = false;
        }
        public bool kiemtra()
        {
            if (txtMaDT.Text == "")
                return false;
            if (txtTenDT.Text == "")
                return false;
            if (txtSoSVToiDa.Text == "")
                return false;
            if (txtSoSVHienhanh.Text == "")
                return false;
            if (txtSoTuanthuchien.Text == "")
                return false;
            if (txtHocKy.Text == "")
                return false;
            if (txtNamhoc.Text == "")
                return false;
            if (gridLookMaLoaiDT.EditValue == null)
                if (gridLookMaLoaiDT.Properties.NullText == "Chọn loại đề tài")
                    return false;
            if (gridLookMaCN.EditValue == null)
                if (gridLookMaCN.Properties.NullText == "Chọn Chuyên Ngành")
                    return false;
            return true;
        }
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (!kiemtra())
            {
                XtraMessageBox.Show("Hãy nhập đầy đủ thông tin","Thông báo");
                return;
            }
            DTO_DeTai detai = new DTO_DeTai();
            detai.MaDT = txtMaDT.Text;
            detai.TenDT = txtTenDT.Text;
            detai.SVToiDa = int.Parse(txtSoSVToiDa.Text);
            detai.SoSVHH = int.Parse(txtSoSVHienhanh.Text);
            if(gridLookMaCN.EditValue ==null)
                detai.MaCN = gridLookMaCN.Properties.NullText;
            else
                detai.MaCN = gridLookMaCN.EditValue.ToString();
            if (gridLookMaLoaiDT.EditValue == null)
                detai.MaLoaiDT = gridLookMaLoaiDT.Properties.NullText;
            else
                detai.MaLoaiDT = gridLookMaLoaiDT.EditValue.ToString();
            detai.SoTuan = txtSoTuanthuchien.Text;
            detai.HocKy = txtHocKy.Text;
            detai.NamHoc = txtNamhoc.Text;
            detai.MaGV = taikhoan.TenTK;
            detai.TrangThai = false;
            detai.NgayCapNhat = DateTime.Now.ToString("yyyy/MM/dd");
            if (ttThemSua)
            {
                if (!BUS.BUS_DeTai.insertDeTai(detai))
                    XtraMessageBox.Show("Lỗi - Mã đề tài đã tồn tại.", "Thông báo");
                else
                    load();
            }
            else
            {
                if (BUS.BUS_DeTai.KiemtraTrangThaiDetai(detai.MaDT))
                {
                    XtraMessageBox.Show("Đề tài đã được duyệt, không thể thay đổi","Thông báo");
                    return;
                }
                if (!BUS.BUS_DeTai.UpdatetDeTai(detai))
                    XtraMessageBox.Show("Lỗi - cập nhật không thành công", "Thông báo");
                else
                    load();
            }
        }

        private void btnXoaDT_Click(object sender, EventArgs e)
        {
            string madt = gvDSDeTai.GetRowCellValue(gvDSDeTai.FocusedRowHandle, "MaDT").ToString();
            if (XtraMessageBox.Show("Bạn có muốn xóa đề tài " + madt + " hay không", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (BUS.BUS_DeTai.KiemtraTrangThaiDetai(madt))
                {
                    XtraMessageBox.Show("Đề tài đã được duyệt, không thể thay đổi", "Thông báo");
                    return;
                }
                if (!BUS.BUS_DeTai.DeleteDeTai(madt))
                    XtraMessageBox.Show("Lỗi - cập nhật không thành công", "Thông báo");
                else
                    load();
            }
        }

        private void btnDongDSDT_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMaDT_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtTenDT.Focus();
        }

        private void txtTenDT_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtSoSVToiDa.Focus();
        }

        private void txtSoSVToiDa_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtSoSVHienhanh.Focus();
        }

        private void txtSoSVHienhanh_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                gridLookUpLoaiDT.Focus();
        }

        private void gridLookMaLoaiDT_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                gridLookUpLoaiCN.Focus();
        }

        private void gridLookMaCN_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtSoTuanthuchien.Focus();
        }

        private void txtSoTuanthuchien_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtHocKy.Focus();
        }

        private void txtHocKy_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtNamhoc.Focus();
        }

        private void txtNamhoc_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnCapNhat.Focus();
        }
    }
}